//
//  ZH_InstitutionsTableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/18.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_InstitutionsTableViewCell.h"
#import "ZH_InstitutionsCollectionViewCell.h"
@interface ZH_InstitutionsTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ZH_InstitutionsTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_InstitutionsTableViewCell";
    ZH_InstitutionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_InstitutionsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
}
- (void)loadSubviewConstraints{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(FITHEIGHT(14));
        make.left.equalTo(self.contentView).offset(FITWITH(11));
        make.right.equalTo(self.contentView).offset(FITWITH(-11));
        make.bottom.equalTo(self.contentView).offset(FITHEIGHT(-15));
    }];
}
///更新 ui
- (void)updateUiForData:(NSMutableArray *)datas
{
    if(datas.count > 0){
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:datas];
        
//        NSInteger item = self.dataArray.count;
//        NSInteger rowNum = (item / 4) + ((item % 4) > 0 ? 1 : 0);
        
        CGFloat collectionHeight = FITSCALE(201);
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(collectionHeight);
        }];
        
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(HEIGHT_TRANSFORMATION(0));
        }];
    }
}
- (void)updataUI{
   
}

#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    if(self.onClickBlock){
//        self.onClickBlock(self.dataArray[indexPath.row][1]);
//    }
//
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake(FITSCALE(142), FITSCALE(201));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return FITHEIGHT(9);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return FITSCALE(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZH_InstitutionsCollectionViewCell *cell = [ZH_InstitutionsCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    cell.layer.cornerRadius = FITSCALE(3);
    cell.layer.borderWidth = FITSCALE(1);
    cell.layer.borderColor = [UIColor colorWithHexString:@"#DFDFDF"].CGColor;
    cell.layer.masksToBounds = YES;
    cell.clipsToBounds = YES;
//    if(self.dataArray.count > 0){
//        [cell updateUiForIcon:self.dataArray[indexPath.row][0] andTitle:self.dataArray[indexPath.row][1]];
//    }
    [cell updateUI:self.dataArray[indexPath.row]];
    return cell;
}



#pragma mark -- getter,setter

///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //        collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [ZH_InstitutionsCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}


///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}

@end
